<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="workForm" :model="model" :rules="validatorRules" slot="detail" >
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="劳动者名称" :labelCol="labelCol"  :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入劳动者姓名"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="手机号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mobile">
              <a-input v-model="model.mobile" placeholder="请输入手机号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="身份证" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="idCard">
              <a-input-number v-model="model.idCard" placeholder="请输入身份证" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
          <a-form-model-item label="劳动二级来源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sourceLaborFirst">
            <j-dict-select-tag type="list" v-model="model.sourceLaborFirst" dictCode="relation_sourceLaborFirst" placeholder="请选择劳动二级来源" />
          </a-form-model-item>
        </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否有经验" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="experience">
              <j-dict-select-tag type="list" v-model="model.experience" dictCode="yes_and_no" placeholder="请选择是否有经验" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="年龄" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="age">
              <a-input-number v-model="model.age" placeholder="请输入年龄" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sex">
              <a-input v-model="model.sex" placeholder="请输入性别"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="劳动者来源地" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sourceAddress">
              <a-input v-model="model.sourceAddress" placeholder="请输入劳动者来源地"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="劳动者来源地编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sourceAddressCode">
              <a-input v-model="model.sourceAddressCode" placeholder="请输入劳动者来源地编码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <!-- 就职单位信息 end-->
          <a-input v-model="model.referrerId" placeholder="推荐人ID"  type="hidden" ></a-input>
          <a-input v-model="model.intentionSiteId" placeholder="意向站点ID" type="hidden"></a-input>
          <a-input v-model="model.preOwnerUserId" placeholder="请输入进入公海前负责人id" type="hidden"  ></a-input>
          <a-input v-model="model.referrerOrgId" placeholder="请输入推荐人归属公司ID" type="hidden"  ></a-input>
          <a-input v-model="model.isConversion" type="hidden"  ></a-input>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'

  export default {
    name: 'WorkForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          isConversion:0
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          mobile: [
            { required: true, message: '请输入手机号!'},
          ],
          name: [
            { required: true, message: '请输入劳动者姓名!'},
          ],
          idCard: [
            { required: true, message: '请输入身份证号!'},
          ],
          experience: [
            { required: true, message: '请选择有无经验!'},
          ],
          sourceLaborFirst: [
            { required: true, message: '请选择二级来源!'},
          ],
        },
        dictOptions:[],
        url: {
          add: "/empWork/add",
          edit: "/empWork/edit",
          queryById: "/empWork/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.initDictConfig();
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      validates (callback){
        this.$refs.workForm.validate(valid => {
          callback(valid);
        })
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      //加载数据字典
      initDictConfig(){
        var parameter = {};
        parameter.deptIds = 141252;
        getAction("/office/empOffice/queryAdminDeptSite",parameter).then((res)=>{
          if (res.success == true) {
            let dictsData = res.result;
            if (dictsData && dictsData.length > 0) {
              for (let i = 0; i < dictsData.length; i++) {
                let item = {};
                item.value = dictsData[i].deptId;
                item.text = dictsData[i].name;
                this.dictOptions.push(item);
                ////console.log(dictoption)
              }
            }
            console.log(this.dictOptions)
          }
        });
      },
      siteChange(e){
        console.log(e);
        this.$set(this.model,"intentionSiteId",e);
        var name;
        for (let i = 0; i < this.dictOptions.length; i++) {
          let item = this.dictOptions[i];
          if(item.value == e){
            name = item.text;
            break;
          }
        }
        this.$set(this.model,"intentionSite",name);

        //查询站点上级
        var parameter = {};
        parameter.deptId = e;
        getAction("/office/empOffice/querySiteSuper",parameter).then((res)=>{
          if (res.success == true) {
            let dictsData = res.result;
            console.log(this.dictsData)
            /*this.$set(this.model,"onCompanyId",dictsData.subjectId);
            this.$set(this.model,"onCompany",dictsData.subjectName);
            this.$set(this.model,"cityCode",dictsData.cityCode);
            this.$set(this.model,"cityName",dictsData.cityName);*/
          }
        });
      }
    }
  }
</script>